// pages/order/list/index.js
import {
  request
} from '../../utils/requestUtils';
Page({
  // 页面的初始数据
  data: {
    refreshTriggered: false,
    total: 0,
    page: {
      pageNum: 1,
      pageSize: 10,
    },
    params: {
      keywords: ''
    },
    dataList: []
  },
  onLoad() {
    // this.getList()
  },
  onShow() {
   this.setData({
    dataList:[]
   })
    this.getList()
  },
  // 触底刷新
  getNext() {
    console.log('触底')
    if (this.data.page.pageNum * this.data.page.pageSize >= this.data.total) {
      return
    } else {
      this.data.page.pageNum++;
      this.getMerchantList();
    }
    this.getList()
  },
  // 下拉刷新
  onScrollRefresh: function () {
    this.setData({
      'page.pageNum': 1,
      dataList:[]
    })
    this.getList()

  },
  // 查询商品信息
  async getList() {
    const result = await request({
      url: `/bus/merchant/banner/list?pageNum=${this.data.page.pageNum}&pageSize=${this.data.page.pageSize}`,
      method: "POST",
      data: {
        title: this.data.params.keywords
      }
    })
    this.setData({
      refreshTriggered: false
    })
    if (result.data.code == 200) {
      var dataList = this.data.dataList.concat(result.data.rows)
      console.log('dataList', dataList)
      this.setData({
        dataList: dataList,
        total: result.data.total
      })
    }
  },
  // 备货完成
  async completed(event){
    const modalRes = await wx.showModal({
      content: '确定备货完成 ?'
    })
    if (modalRes.cancel) return
    const {id} = event.currentTarget.dataset
    const result = await request({
      url: `/merchant/order/api/updateStatus`,
      method: "PUT",
      data:{
        id: id
      }
    })
    if (result.data.code == 200) {
      this.onScrollRefresh()
    }
  },
  // 详情
  toOrderDetail(event){
    console.log('event', event)
    wx.navigateTo({
      url: '/pages/merchant_banner_add/index?id='+event.currentTarget.dataset.id
    })
  },
  // 新增
  addSHow(){
    wx.navigateTo({
      url: '/pages/merchant_banner_add/index'
    })
  }
})
